import { ApiResponse, profileType, profileUseType } from '@/types/data';
import { rootThunkAction } from '@/types/store';
import http from '@/utils/request';
//获取用户信息的action==>先定义action类型
export const getProfile = (): rootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<ApiResponse<profileType>>('/user');
    dispatch({
      type: 'profile',
      payload: res.data.data,
    });
  };
};

// 获取用户个人资料
export const getProfileUse = (): rootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<ApiResponse<profileUseType>>('/user/profile');
    // console.log(res);
    dispatch({
      type: 'profile/use',
      payload: res.data.data,
    });
  };
};

// 编辑用户个人资料
export const changeProfile = (
  data: Partial<profileUseType>
): rootThunkAction => {
  return async (dispatch) => {
    await http.patch('/user/profile', data);
    // 这里更新完成后,直接刷新页面,不需要在组件中在执行
    dispatch(getProfileUse());
  };
};

// 更新图片信息
export const updatePhoto = (fd: FormData): rootThunkAction => {
  return async (dispatch) => {
    await http.patch('/user/photo', fd);
    //重新获取用户信息
    dispatch(getProfileUse());
  };
};
